classname(datatype arg1,...) { super(args..); statements --------- }
class Set { protected int n1,n2; public Set(int x,int y) { n1=x; n2=y; } public int sum() { int s=n1+n2; return s; } public double mean() { double m=sum()/2.0; return m; } } class XSet extends Set { protected int n3; public XSet(int x,int y,int z) { super(x,y); n3=z; } public int sum() { int s=super.sum()+n3; return s; } public double mean() { double m=sum()/3.0; return m; } } class demo { public static void main(String args[]) { Set a=new Set(4,5); System.out.println("Sum of set a is "+a.sum()); System.out.println("Mean of set a is "+a.mean()); XSet b=new XSet(1,2,4); System.out.println("Sum of set b is "+b.sum()); System.out.println("Mean of set b is "+b.mean()) ; } }
Sum of set a is 9 Mean of set a is 4.5 Sum of set b is 7 Mean of set b is 2.333333